package com.supermap.business.farmland;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.farmland.FarmlandService;
import com.supermap.dubbo.api.farmland.entity.Farmland;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * @Author:langhongshuai
 * @Description:农田接口层
 * @Date:18/1/29 上午9:26
 */
@Controller
@RequestMapping("FarmlandController")
public class FarmlandController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(FarmlandController.class);

    @Reference(timeout = 100000)
    private FarmlandService farmlandService;

    /**
     * 通过用户id和区域code获得用户的农田信息
     */
    @RequestMapping("/getFarmlandByUIdArea")
    @ResponseBody
    public JSONUtil getFarmlandByUIdArea(String userId, String areaCode,Integer pageNum,Integer pageSize){
        return farmlandService.getFarmlandByUIdArea(userId,areaCode,pageNum,pageSize);
    }

    /**
     * 增加农田
     * @param request
     * @return
     */
    @RequestMapping("/addFarmland")
    @ResponseBody
    public JSONUtil addFarmland(HttpServletRequest request) throws UnsupportedEncodingException {
        String userId = request.getParameter("userId");
        String flLongitude = request.getParameter("longitude");
        String flLatitude = request.getParameter("latitude");
        String flheight = request.getParameter("height");
        String flAreaCode = request.getParameter("areaCode");
        String flArea = request.getParameter("area");
        String flCoordinates = request.getParameter("coordinates");
        String flDescript = request.getParameter("descript");
        String crop = request.getParameter("crop");
        String farmname = request.getParameter("farmname");
        Farmland farmland = new Farmland();
        farmland.setUserId(Integer.parseInt(userId));
        farmland.setLatitude(Double.parseDouble(flLatitude));
        farmland.setLongitude(Double.parseDouble(flLongitude));
        farmland.setHeight(Integer.parseInt(flheight));
        farmland.setCode(flAreaCode);
        farmland.setArea(Double.parseDouble(flArea));
        farmland.setCoordinates(flCoordinates);
        farmland.setDescript(flDescript);
        farmland.setCrop(crop);
        farmland.setFarmname(farmname);
        return farmlandService.addFarmland(farmland);
    }

    /**
     * 删除农田
     * @param request
     * @return
     */
    @RequestMapping("/delFarmland")
    @ResponseBody
    public JSONUtil delFarmland(HttpServletRequest request){
        String farmlandId = request.getParameter("farmlandId");
        return farmlandService.delFarmland(Integer.parseInt(farmlandId));
    }

    /**
     *通过区域和作物id获取农田信息
     * @param request
     * @return
     */
    @RequestMapping(value="/getFramlandByCropIdAndAreaCode",method = RequestMethod.POST)
    @ResponseBody
    public JSONUtil getFramlandByCropIdAndAreaCode(HttpServletRequest request){
        String cropId = request.getParameter("cropId");
        String areaCode = request.getParameter("areaCode");
        return farmlandService.getFramlandByCropIdAndAreaCode(cropId,areaCode);
    }


    /**
     *通过区域code获取农田分布
     * @param request
     * @return
     */
    @RequestMapping(value="/getFarmLandDistribution",method = RequestMethod.GET)
    @ResponseBody
    public JSONUtil getFarmLandDistribution(HttpServletRequest request){
        String areaCode = request.getParameter("areaCode");
        return farmlandService.getFarmLandDistribution(areaCode);
    }

    /**
     * 通过农田ID获取农事信息和病虫害信息-
     * @param farmlandid
     * @return
     */
    @RequestMapping(value = "/getFarmingAndDIPInfoByFarmlandId",method = RequestMethod.GET)
    @ResponseBody
    public JSONUtil getFarmingAndDIPInfoByFarmlandId(String farmlandid){
        return farmlandService.getFarmingAndDIPInfoByFarmlandId(Integer.parseInt(farmlandid));
    }

    /**
     * 通过作物code获取农事信息和病虫害信息
     * @param cropCode
     * @return
     */
    @RequestMapping(value = "/getAllDisasterDIPByCCodeAndUId",method = RequestMethod.GET)
    @ResponseBody
    public JSONUtil getAllDisasterDIPByCCodeAndUId(String cropCode,String userId){
        RpcContext.getContext().setAttachment("userId",userId);
        farmlandService.getAllDisasterDIPByCCodeAndUId(cropCode,Integer.parseInt(userId));
        return farmlandService.getAllDisasterDIPByCCodeAndUId(cropCode,Integer.parseInt(userId));
    }

    /**
     * 通过用户id获取用户所种植的农田里的作物种类
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getCropsByUserId",method = RequestMethod.GET)
    @ResponseBody
    public JSONUtil getCropsByUserId(String userId){
        return farmlandService.getCropsByUserId(userId);
    }

    /**
     * 修改农田信息
     * @param farmland
     * @return
     */
    @RequestMapping(value = "/updateFarmland",method = RequestMethod.POST)
    @ResponseBody
    public JSONUtil updateFarmland(@RequestBody Farmland farmland){
        return farmlandService.updateFarmland(farmland);
    }
}
